//1. set this to the desired path
cd "~/Desktop/LAP_PSRM_Replication/"

//2. Import the Garlick (2017) replication data  with the additional roll call votes from main analysis
use "LAP_Replication_GarlickPlus_30states.dta", clear

//3. Find and keep the passage votes
gen votes = 1
keep if sg_v == 2

//4. Find the average party difference on the state legislative years 
collapse (mean) party_diff (sum) votes, by(state chamber year1)

//5. Limit the sample and prepare data to match Shor and Holyoke data
drop if year1 < 2006 
replace state = upper(state)

//6. Merge in Shor McCarty 2015 data
merge m:1 state year1 using "LAP_Replication_ShorMcCarty_50states.dta"

//7. Averager the distance between party medians across the two chambers
egen avgdiff = rowmean(h_diffs s_diffs)

//8. Create the upper chamber if there is only a lower chamber score
expand 2 if _m == 2, gen(dupes)
replace chamber = "lower" if _m == 2 & dupes == 0
replace chamber = "upper" if _m == 2 & dupes == 1
drop _m dupes

//9. Bring in the Holyoke data on interest groups
merge m:1 year1 state using "LAP_Replication_Holyoke_50states.dta"
drop _m

//10. Make the shor data fit the two line structure.
gen diffs = .
replace diffs = h_diffs if chamber == "lower"
replace diffs = s_diffs if chamber == "upper"
label var diffs "Distance between party medians (Shor McCarty 2015)"
drop h_diffs s_diffs

//11. Transform the holyoke data into its natural log and using the Laplace smoothing procedure
foreach v of varlist holy* {
gen l`v' = log(`v' + 1)
}

//12. Variable labels
label var lholy_B "Businesses (log)"
label var lholy_NP "Non-profits (log)"
label var lholy_G "Governments (log)"


//13. Highlight the states from the main analysis
gen stlocal = .
replace stlocal = 1 if state == "CO" | state == "WI" | state == "OH"


//14. Create Figure 3
twoway ///
(scatter party_diff lholy_B if stlocal == . , mcolor(red%80)) ///
(scatter party_diff lholy_B if stlocal == 1 , msymbol(O) mcolor(red%80)) ///
(scatter party_diff lholy_NP if stlocal == ., mcolor(blue%80)) ///
(scatter party_diff lholy_NP if stlocal == 1 , msymbol(D) mcolor(blue%80)) ///
(lfit party_diff lholy_B, lcolor(red%80)) ///
(lfit party_diff lholy_NP, lpattern(solid) lcolor(blue%80)), ///
legend( order (1 "Businesses" 5 "Fit" 3 "Non-profits" 6 "Fit")) ///
xtitle("Number of Groups (log)") ytitle("Session Average Party Difference")



//15. Run the regressions for Table 8 (first transform year)
egen yrno = group(year1)

eststo clear
eststo: reghdfe party_diff lholy_* if chamber == "lower" & year >= 2009 & year <= 2014 & votes >= 10, absorb(yrno ) cluster(state)
eststo: reghdfe party_diff lholy_* if chamber == "upper" & year >= 2009 & year <= 2014 & votes >= 10, absorb(yrno ) cluster(state)
eststo: reghdfe diffs lholy_* if chamber == "lower" , absorb(yrno ) cluster(state)
eststo: reghdfe diffs lholy_* if chamber == "upper" , absorb(yrno ) cluster(state)

//16. Create Table 8
esttab , se label b(2) se (2) star(* 0.05 ** 0.01) replace tex

//17. Limit the sample for party difference only to run the Granger tests
drop if votes == . 
drop if year >= 2015
drop if year < 2009


//18. Collapse down to replicate the granger analysis

collapse (min) minvotes=votes (max) maxvotes=votes (firstnm) holy* lholy* (mean) party_diff [fweight=votes], by(state year1)

//19. Drop sessions with unusually low numbers of recorded votes
replace party_diff = . if maxvotes < 10

//20. Turn the data into a panel by using the Time Series commands (first transform strings to numeric)
egen fips = group(state)
egen yrno = group(year1)
xtset fips yrno

//21. Run a dummy regression to set the appropriate sample for every run
reghdfe party_diff l(1/2).lholy_B, absorb(fips) cluster(state)
gen insampreg2 = e(sample)

//22. Relabel the independent variables
label var lholy_B "Businesses (log)"
label var lholy_NP "Non-profits (log)"
label var lholy_G "Governments (log)"
label var party_diff "Party Diff. (Avg.)"


//22. Run the Granger Tests

eststo clear
//Granger Tests FE
local i = 1
global yvar "party_diff"
local xvars "lholy_N lholy_B lholy_G"
foreach xvar of local xvars{
 eststo: reghdfe $yvar L(1/2).$yvar L(1/2).`xvar' if insampreg2 , absorb(yrno state) cluster(state)
testparm L(1/2).`xvar' 
di "test `i'"
local i = `i' + 1
}

local xvar "party_diff"
local yvars "lholy_N lholy_B lholy_G"
foreach yvar of local yvars{
 eststo: reghdfe `yvar' L(1/2).`yvar' L(1/2).`xvar' if insampreg2, absorb(yrno state) cluster(state)
testparm L(1/2).`xvar' 
di "test `i'"
local i = `i' + 1
}

//23: Table 4: F-tests and P-values are pulled from the above Stata Output. 

//24. Table 9: (see Table 4 instructions above for f-tests and p-values
esttab, se label se(2) b(2) star( * 0.05) ///
mtitles("DV: PD" "DV: PD" "DV: PD" "DV: NP" "DV: B" "DV: G")


